﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuantitativeInvestment.Bean;
using QuantitativeInvestment.Tools;

namespace QuantitativeInvestment.Factor
{
    class MacdFactor : Factor
    {
        public MacdFactor()
        {
            this.name = "平滑异同平均";
            Parameter p1 = new Parameter("短期", 12);
            this.paraList.Add(p1.name, p1);

            Parameter p2 = new Parameter("长期", 26);
            this.paraList.Add(p2.name, p2);

            Parameter p3 = new Parameter("信号期", 10);
            this.paraList.Add(p3.name, p3);
        }

        public override void addFactorValue(Stock stock)
        {
            int fastPeriod = Int32.Parse(this.paraList["短期"].value.ToString());
            int slowPeriod = Int32.Parse(this.paraList["长期"].value.ToString());
            int signalPeriod = Int32.Parse(this.paraList["信号期"].value.ToString());

            if (!stock.factors.ContainsKey(this.name + this.paraList["短期"].value.ToString() + this.paraList["长期"].value.ToString() + this.paraList["信号期"].value.ToString()))
            {
                TaLib lib = new TaLib();

                double[,] MacdValues = lib.getMACD(stock.factors["价格"], fastPeriod, slowPeriod, signalPeriod);
                int length = stock.factors["价格"].Length;
                double[] result1 = new double[length];
                double[] result2 = new double[length];
                double[] result3 = new double[length];

                for (int i = 0; i < length; i++)
                {
                    result1[i] = MacdValues[0, i];
                    result2[i] = MacdValues[1, i];
                    result3[i] = MacdValues[2, i];
                }

                stock.factors.Add(this.name + this.paraList["短期"].value.ToString() + "Macd", result1);
                stock.factors.Add(this.name + this.paraList["信号期"].value.ToString() + "Macd信号", result2);
                stock.factors.Add(this.name + this.paraList["长期"].value.ToString() + "Macd长期", result3);

            }
        }
    }
}
